% takes output of widefieldProcessingScripts (gcampFilename), normalizes and scales the
% image to the high resolution image ultimately to use for quantification
% highResVasc is a high resolution image of the vasculature; note it needs
% to be oriented in the 2p orientation
% inputs:
% gcampFilename = filename (preferably complete file path) to the output of
% the processing of multisessionGcampStats and generalStatsCalculation
%
% highResVasc = reference high resolution image of the vasculature that
% everything in the pipeline is ultimately mapped to. Should be in the 2p
% orientation
% 
% dosave = true if you want it to automatically be saved
% 
% lowResFilename = the low resolution image of the vasculature 
%
% updates from measuredGCaMP_normalize:
% basically doing exactly the same thing, but now the input is the averaged
% gcamp across multiple sessions so some of the filenames etc are going to
% be a bit different
%
% camtype is either 'dalsa' or 'thorcam'; these cameras have different
% orientation relative to the 2p and I'm using different magnification

function [avGCAMP_normalized_scaled,windowmask,testregister,gcamp_normalized] = measuredGCaMP_normalize_v2_0(gcampFilename,highResVasc,dosave,lowResFilename,camtype,mag)

if nargin < 5 || isempty(camtype)
    camtype = 'dalsa';          %this will change how images are flipped/registered...
end
if nargin < 6 || isempty(mag)
    switch camtype
        case 'dalsa'
            mag = 1;
        case 'thorcam'
            mag = 3;
    end
end
if nargin < 3 || isempty(dosave)
    dosave = true;
end
switch camtype
    case 'thorcam'
        if nargin < 4 || isempty(lowResFilename)
            load(gcampFilename,'vascReference');
            lowRes = fliplr(vascReference);
        else
            lowRes = fliplr(double(imread(lowResFilename)));
        end
    case 'dalsa'
        if nargin < 4 || isempty(lowResFilename)
            load(gcampFilename,'vascReference');
            lowRes = flipud(vascReference);
        else
            lowRes = flipud(double(imread(lowResFilename)));
        end
end

load(gcampFilename,'avgResponse');

minprctile = 15;
maxprctile = 99;
avgResponse(avgResponse==0) = nan;
gcamp_normalized = avgResponse;

gcamp_normalized = (gcamp_normalized-prctile(gcamp_normalized(:),minprctile))/(prctile(gcamp_normalized(:),maxprctile)-prctile(gcamp_normalized(:),minprctile));
gcamp_normalized(gcamp_normalized<0) = 0;

temp = imgaussfilt(lowRes,20);lowRes = lowRes./temp;        %flatten image
highResVasc = double(highResVasc);
highResVasc = (highResVasc-prctile(highResVasc(:),1))/(prctile(highResVasc(:),98)-prctile(highResVasc(:),1));
highResVasc(highResVasc<0) = 0;

[MOVINGREG] = registerImages(lowRes,highResVasc,mag);
testregister = imwarp(imresize(lowRes,mag),MOVINGREG.Transformation,'OutputView',MOVINGREG.SpatialRefObj);

Rreg = zeros(size(highResVasc,1),size(highResVasc,2),size(gcamp_normalized,3));
for n = 1:size(gcamp_normalized,3)
    switch camtype
        case 'thorcam'
            cur = fliplr(gcamp_normalized(:,:,n));cur(isnan(cur)) = 0;
        case 'dalsa'
            cur = flipud(gcamp_normalized(:,:,n));cur(isnan(cur)) = 0;
    end
    cur = imwarp(imresize(cur,mag),MOVINGREG.Transformation,'OutputView',MOVINGREG.SpatialRefObj);
    Rreg(:,:,n) = cur;
end
avGCAMP_normalized_scaled = Rreg;
figure
imagesc(highResVasc);axis equal
disp('select window ROI')
roi = drawpolygon;
windowmask = createMask(roi);
for n = 1:size(avGCAMP_normalized_scaled,3)
    avGCAMP_normalized_scaled(:,:,n) = avGCAMP_normalized_scaled(:,:,n).*windowmask;
end
if dosave
    save(gcampFilename,'avGCAMP_normalized_scaled','windowmask','-append')
end
